﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZYStudios.Models;
using ZYStudios.Filter;

namespace ZYStudios.Controllers
{
    [DataForMasterPageFilter]
    public class GuestBookController : BaseController
    {
        //
        // GET: /GuestBook/

        public ActionResult Index(int? id = 1)
        {
            ZYStudios.Models.Data aData = new Models.Data();
            ZYStudios.Models.DataTableAdapters.MessageTableAdapter aMessagesAdapter = new Models.DataTableAdapters.MessageTableAdapter();
            aMessagesAdapter.Fill(aData.Message);
            List<UserMessageEditModel> MessagesList = new List<UserMessageEditModel>();
            foreach (var row in aData.Message.ToArray())
            {
                MessagesList.Add(new UserMessageEditModel()
                {
                    ID = row.ID,
                    Content = row.Content,
                    MessageDate = row.MessageDate,
                    UserName = row.UserName,
                    UserPhone = row.UserPhone,
                    UserQQ = row.UserQQ,
                    UserEmail = row.UserEmail,
                    Reply = row.Reply
                });
            }

            var query = (from c in MessagesList
                         select c);
            var PageSize = 20;

            query = query.OrderByDescending(p => p.MessageDate);

            ViewData["Messages"] = query.Skip(PageSize * Convert.ToInt32(id - 1))
                                .Take(PageSize).ToList();

            var count = query.Count();
            ViewData["Current"] = id;
            ViewData["Count"] = (count % PageSize == 0) ? (count / PageSize) : count / PageSize + 1;
            ViewData["PageSize"] = PageSize;

            ViewData["ErrorMsg"] = Session["ErrorMsg"];

            return View();
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult PostMessage(string pUserName, string pUserQQ, string pUserEmail, string pUserPhone, string pContent, string pCheckCode)
        {
            Session["ErrorMsg"] = "";
            if (Session["CheckCode"] != null && Session["CheckCode"].ToString() == pCheckCode)
            {
                ZYStudios.Models.Data aData = new Models.Data();
                ZYStudios.Models.DataTableAdapters.MessageTableAdapter aMessagesAdapter = new Models.DataTableAdapters.MessageTableAdapter();
                aMessagesAdapter.Fill(aData.Message);


                aData.Message.AddMessageRow(new HtmlString(pUserName).ToHtmlString(), new HtmlString(pContent).ToHtmlString(), new HtmlString(pUserQQ).ToHtmlString(), new HtmlString(pUserEmail).ToHtmlString(), new HtmlString(pUserPhone).ToHtmlString(), DateTime.Now, "");

                aMessagesAdapter.Update(aData.Message);

                Response.RemoveOutputCacheItem("/GuestBook/Index.html");

                Session["ErrorMsg"] = null;
            }
            else
            {
                Session["ErrorMsg"] = "验证码错误!";
            }

            return RedirectToAction("Index");
        }
    }
}
